<?php

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/


class DB  {
	
	//var $dberror = '';
	var $count_query = 0;
	var $work_time = '';
	//var $last_query = '';
	var $query_id = false;
	var $time_before = 0;
	var $mysql_version = 0;
	
	var $dbserver;
	var $dbuser;
	var $dbpass;
	var $dbname;

	function __construct ($dbserver, $dbuser, $dbpass, $dbname) 
	{
		$this->dbserver = $dbserver;
		$this->dbuser = $dbuser;
		$this->dbpass = $dbpass;
		$this->dbname = $dbname;
	}

	function connect () 
	{
		$mysqlconnect = mysql_connect ($this->dbserver, $this->dbuser, $this->dbpass);
		if (!$mysqlconnect) 
		{
			print '<h1>Ошибка подключения к БД!</h1>';
			exit;
		}

		$mysqlselect = mysql_select_db ($this->dbname, $mysqlconnect);
		if (!$mysqlselect) 
		{
			print '<h1>В настоящее время БД не доступна!</h1>';
			exit;
		}
		
		$this->mysql_version = mysql_get_server_info();

		if (version_compare($this->mysql_version, '4.1', ">=")) mysql_query("/*!40101 SET NAMES 'utf-8' */");
		
		/*mysql_query ("set character_set_client='cp1251'");
		mysql_query ("set character_set_results='cp1251'");
		mysql_query ("set collation_connection='cp1251_general_ci'");*/
	}
	
	function query ($string) 
	{
		$time_before = 0;
		$time_before += $this->get_time();
		$this->count_query++;
		
		$this->last_query = $string;
		
		if (!$this->query_id = @mysql_query ($string)) $this->log_error (mysql_error (), $string);
		
		$this->work_time += $this->get_time() - $time_before;
		
		return $this->query_id;
	}
	
	function fetch_array ($value) 
	{
		$time_before = 0;
		$time_before += $this->get_time();
		
		$this->work_time += $this->get_time() - $time_before;
		return @mysql_fetch_array ($value);
	}
	
	function fetch_row ($value) 
	{
		$time_before = 0;
		$time_before += $this->get_time();
		
		$this->work_time += $this->get_time() - $time_before;
		return @mysql_fetch_row ($value);
	}
	
	function num_rows ($value) 
	{
		$time_before = 0;
		$time_before += $this->get_time();
		
		$this->work_time += $this->get_time() - $time_before;
		return @mysql_num_rows ($value);
	}
	
	function log_error ($error, $string) 
	{	
		$dt = date('Y-m-d H:i:s');
		$myip=getenv('REMOTE_ADDR');
		$err = '<error>\r\n';
		$err .= '<date>' . $dt . '</date>\r\n';
		$err .= '<ip>' . $_SERVER['REMOTE_ADDR'] . '</ip>\r\n';
		$err .= '<errno>13</errno>\r\n';
		$err .= '<type>MySQL error</type>\r\n';
		$err .= '<msg>' . $error . ' <br /> ' . $string . '</msg>\r\n';
		$err .= '<urlstring>' . $_SERVER['REQUEST_URI'] . '</urlstring>\r\n';
		$err .= '<script>none information</script>\r\n';
		$err .= '<line>none information</line>\r\n';
		$err .= '</error>\r\n';
		
		log_error($err);
		ob_end_clean ();
		include '500.html';
		exit();
	}
	
	function past_work_time () 
	{
		return $this->work_time;
	}
	
	function get_time()
	{
		$this_time = microtime(); 
		$time_array = explode(' ',$this_time); 
		return ($time_array[1] + $time_array[0]); 
	}

}

?>
